
package com.neolix.watch.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class MagicTextView extends TextView {
    // 递减/递增 的变量值
    private double mRate;
    // view 设置的值
    private double mValue;
    // 当前显示的值
    private double mCurValue;
    // 当前变化后最终状态的目标值
    private double mGalValue;
    // 控制加减法
    private int rate = 1;
    private boolean refreshing;
    private static final int REFRESH = 1;
    private static final int SCROLL = 2;
    DecimalFormat fnum = new DecimalFormat("0");//"0.00"

    private MagicTextViewChangListener changeListener;

    private int minWidth;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case REFRESH:
                    if (rate * mCurValue < mGalValue) {
                        refreshing = true;
                        setText(fnum.format(mCurValue));
                        mCurValue += mRate * rate;
                        mHandler.sendEmptyMessageDelayed(REFRESH, 70);

                        Log.i("lixiang getwidth", String.valueOf(getWidth()));

                        if (getWidth() > minWidth)
                            minWidth = getWidth();

//                        setMinWidth(minWidth);

                        Log.i("lixiang", String.valueOf(minWidth));
                    } else {
                        refreshing = false;
                        setText(fnum.format(mGalValue));
                        if (changeListener != null)
                            changeListener.completeChange();
                    }
                    break;

                case SCROLL:
                    doScroll();
                    break;

                default:
                    break;
            }
        }
    };

    public MagicTextView(Context context) {
        super(context);
    }

    public MagicTextView(Context context, AttributeSet set) {
        super(context, set);
    }

    public MagicTextView(Context context, AttributeSet set, int defStyle) {
        super(context, set, defStyle);
    }

    public void setValue(double value) {
        mCurValue = 0.00;
        mGalValue = isShown() ? value : 0;
        mValue = value;
        mRate = mValue / 20.00;
        BigDecimal b = new BigDecimal(mRate);
        mRate = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        minWidth = 1;
        setMinWidth(minWidth);
    }

    public void scrollChanged() {
        Message msg = mHandler.obtainMessage();
        msg.what = SCROLL;
        mHandler.sendMessageDelayed(msg, 500);
    }

    public void setChangeListener(MagicTextViewChangListener listener) {
        this.changeListener = listener;
    }

    private void doScroll() {
        if (refreshing)
            return;

        rate = 1;
        mGalValue = mValue;
        mHandler.sendEmptyMessage(REFRESH);
        if (changeListener != null)
            changeListener.beginChange();
    }

    public interface MagicTextViewChangListener {
        void beginChange();

        void completeChange();
    }
}

